package cn.edu.sjtu.se.dslab.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import cn.edu.sjtu.se.dslab.action.bean.PageBean;
import cn.edu.sjtu.se.dslab.component.data.AvionicsEquipment;
import cn.edu.sjtu.se.dslab.component.data.EquipmentCategory;
import cn.edu.sjtu.se.dslab.dao.IAvionicsEquipmentDao;
import cn.edu.sjtu.se.dslab.dao.IBaseDao;
import cn.edu.sjtu.se.dslab.dao.IDomainEntityDao;
import cn.edu.sjtu.se.dslab.exception.DBException;
import cn.edu.sjtu.se.dslab.util.Const;
import cn.edu.sjtu.se.dslab.util.MD5;

@Repository("avionicsEquipmentDao")
public class AvionicsEquipmentDaoImpl implements IAvionicsEquipmentDao {

	@Resource(name = "baseDao")
	private IBaseDao<AvionicsEquipment> baseDao;
	
	@Resource(name="domainEntityDao")
	private IDomainEntityDao domainEntityDao;

	private static final String URI = "uri";
	private static final String NAME = "name";
	private static final String EQUIPTYPE = "equiptype";
	
	private static final String DELETED = "deleted";
	
	@Override
	public void save(AvionicsEquipment equipment) {
		String temp = equipment.getName()
				+ "avionics_equipment"+System.currentTimeMillis();
		MD5 md5 = new MD5();
		String uri = md5.getMD5ofStr(temp).substring(0, 8);
		while (!domainEntityDao.checkUri(uri)) {
			temp = equipment.getName()
					+ "avionics_equipment"+System.currentTimeMillis();
			uri = md5.getMD5ofStr(temp).substring(0, 8);
		}
		equipment.setUri(Const.URI_AVIONICS_EQUIPMENT + ":" + uri);
		baseDao.save(equipment);
	}

	@Override
	public void update(AvionicsEquipment equipment) {
		baseDao.update(equipment);
	}

	@Override
	public AvionicsEquipment queryById(long id) {
		return baseDao.queryById(AvionicsEquipment.class, id);
	}

	@Override
	public List<AvionicsEquipment> queryAll() {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from AvionicsEquipment where ");
		hql.append(DELETED + "=0");
		return baseDao.query(hql.toString());
	}

	@Override
	public List<AvionicsEquipment> queryByEquipmentCategory(EquipmentCategory category) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from AvionicsEquipment a where ");
		hql.append(" a.category.id=");
		hql.append("" + category.getId());
		hql.append(" and ");
		hql.append(DELETED + "=0");
		return baseDao.query(hql.toString());
	}

	@Override
	public PageBean<AvionicsEquipment> queryByPageNum(int pageNo)
			throws DBException {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from AvionicsEquipment where ");
		hql.append(DELETED + "=0");
		PageBean<AvionicsEquipment> bean = baseDao.queryByspecialHqlForPage(pageNo,
				Const.PAGESIZE, hql.toString());
		return bean;
	}

	@Override
	public AvionicsEquipment queryByUri(String uri) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();

		// 拼接sql语句
		hql.append("from AvionicsEquipment where ");
		hql.append(URI + "=");
		hql.append("'" + uri + "' and ");
		hql.append(DELETED + "=0");
		return baseDao.queryObject(hql.toString());
	}

	@Override
	public List<AvionicsEquipment> queryByType(String type) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from AvionicsEquipment where ");
		hql.append(EQUIPTYPE + "=");
		hql.append("'" + type + "' and ");
		hql.append(DELETED + "=0");
		return baseDao.query(hql.toString());
	}

	@Override
	public AvionicsEquipment queryByName(String name) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from AvionicsEquipment where ");
		hql.append(NAME + "=");
		hql.append("'" + name + "' and ");
		hql.append(DELETED + "=0");
		return baseDao.queryObject(hql.toString());
	}

	@Override
	public List<AvionicsEquipment> queryByProperty(String text) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from AvionicsEquipment a where ");
		hql.append(" a.name like '%");
		hql.append( text+ "%' or ");
		hql.append(" a.cnName like '%");
		hql.append( text+ "%' or ");
		hql.append(" a.description like '%");
		hql.append( text+ "%'  and ");
		hql.append(DELETED + "=0");
		return baseDao.query(hql.toString());
	}

}
